﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using RibbonEditor.Rules;
using System.Windows.Forms;
using RibbonEditor.AppCode;
using RibbonEditor.Forms.Rules;

namespace RibbonEditor
{
    public class TabDisplayRule : IXmlBuildable, IChildDeletable
    {
        public string TabCommand { get; set; }
        public List<ITabDisplayRule> Rules { get; set; }

        public TabDisplayRule()
        {
            this.Rules = new List<ITabDisplayRule>();
        }

        public TabDisplayRule(XmlNode node) : this()
        {
            try
            {
                this.TabCommand = (string)XmlHelper.ExtractXmlAttributeToProperty2(node, "TabCommand", XmlHelper.PropertyType.stringType);

                foreach (XmlNode ruleNode in node.ChildNodes)
                {
                    ITabDisplayRule dRule = RuleHelper.GetITabDisplayRuleFromXml(ruleNode);
                    this.Rules.Add(dRule);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public string GetTabCommand()
        {
            return this.TabCommand;
        }

        public void SetTabCommand(string tabCommand)
        {
            this.TabCommand = tabCommand;
        }

       
        public void AddNode(XmlNode tabdisplayRulesNode)
        {
            try
            {
                XmlNode tabdisplayRuleNode = XmlHelper.AddNode(tabdisplayRulesNode, "TabDisplayRule");
                XmlHelper.AddAttribute(tabdisplayRuleNode, "TabCommand", this.TabCommand);

                foreach (ITabDisplayRule rule in this.Rules)
                {
                    rule.AddNode(tabdisplayRuleNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("TabDisplayRule");
                node.ContextMenuStrip = BuildContextMenu(node);
                node.Tag = this;

                foreach (ITabDisplayRule rule in this.Rules)
                {
                    rule.BuildTreeViewElement(node);
                }

                node.Expand();

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode enableRuleNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = enableRuleNode;

            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Rule");
            tsmi.Click += new EventHandler(tsmi_Click);
            cms.Items.Add(tsmi);

            ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);
            ContextMenuStripHelper.AddDeleteMenu(cms, true);

            //ToolStripSeparator tsms = new ToolStripSeparator();
            //ToolStripMenuItem tsmd = new ToolStripMenuItem("Delete");
            //cms.Items.Add(tsms);
            //cms.Items.Add(tsmd);

            return cms;
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                RuleSelectionDialog rsDialog = new RuleSelectionDialog(RuleSelectionDialog.RuleType.TabDisplayRule);
                rsDialog.StartPosition = FormStartPosition.CenterParent;

                if (rsDialog.ShowDialog() == DialogResult.OK)
                {
                    ITabDisplayRule selectedRule = (ITabDisplayRule)rsDialog.SelectedRule;

                    this.Rules.Add(selectedRule);
                    selectedRule.BuildTreeViewElement(selectedNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is ITabDisplayRule)
                {
                    this.Rules.Remove((ITabDisplayRule)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
